package com.example.toolproject.demo.controller;

import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.service.DataSearchService;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName LogOutController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-09-08 13:19
 **/
@Controller
@CrossOrigin
@RequestMapping("/logOut")
public class LogOutController {

    @Autowired
    private DataSearchService dataSearchService;
    /**
     * 用户退出
     * @return
     */
    @GetMapping("/user")
    @ResponseBody
    public ResultBean getLogOutUser(HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        /**
         * 获取用户id以及角色
         */
         int userId = u.getId();
         int roleId = u.getRoleId();
         if(roleId == 1 || roleId == 2){
             return ResultBean.success("退出成功");
         }else{
             /**
              * 删除导入的文化资源信息
              */
             return dataSearchService.deleteDataSearch(u);
         }
    }
}
